package com.ym.b2b.tags;

import java.util.List;

import org.apache.commons.lang.StringUtils;

import com.ym.b2b.tags.base.BaseTag;
import com.ym.b2b.type.service.EmBNewsTypeService;
import com.ym.b2b.type.sqlbean.EmBNewsType;
import com.zc.commons.spring.SpringContextHolder;

/**
 * 内容分类 获取
 * 
 * @ClassName: CmsTypeTags
 * @Description: TODO
 * @Author 竺夏军
 * @Date 2015年1月28日 下午3:37:53
 * @Modify
 * @CopyRight 信息化团队
 */
public class CmsCategoryTags extends BaseTag {

    private static final long serialVersionUID = 895777383599438520L;

    protected String          typeName;                              // 分类名

    protected Long            typeId;                                // 分类ID
    protected String          getType;                               // list or detail
    protected Long            newsTypeId;                            // 跟getType detail时成对

    public int doStartTag() {

        // 判断传入类型
        if (StringUtils.isBlank(getGetType())) {
            return EVAL_BODY_INCLUDE;
        }
        if (getGetType().equals("list")) {
            EmBNewsTypeService emBNewsTypeService = SpringContextHolder.getBean("emBNewsTypeService");
            EmBNewsType emBNewsType = new EmBNewsType();
            emBNewsType.setTypeId(getTypeId());
            emBNewsType.setTypeName(getTypeName());

            // 设置缓存KEY
            setCache(emBNewsType);
            List<EmBNewsType> list = emBNewsTypeService.searchEmBNewsTypeByParentIdToComboTree(emBNewsType);

            getPageContext().getRequest().setAttribute(getOutResponseAttrName(), list);
        }

        if (getGetType().equals("detail")) {
            if (getNewsTypeId() == null || "".equals(getNewsTypeId())) {

            } else {
                // 获取明细
                EmBNewsTypeService emBNewsTypeService = SpringContextHolder.getBean("emBNewsTypeService");
                EmBNewsType emBNewsType = emBNewsTypeService.showCmsTypeDetail(getNewsTypeId());

                if (null != emBNewsType && null != emBNewsType.getParentid()) {
                    // 获取parentName 层级
                    EmBNewsType emBNewsTypes = new EmBNewsType();
                    emBNewsTypes.setNewsTypeId(emBNewsType.getParentid());
                    emBNewsTypes = emBNewsTypeService.searchEmBNewsType(emBNewsTypes);
                    if (null != emBNewsTypes) {
                        emBNewsType.setParentName(emBNewsTypes.getName());
                    }
                }
                getPageContext().getRequest().setAttribute(getOutResponseAttrName(), emBNewsType);
            }

        }
        return EVAL_BODY_INCLUDE;

    }

    // 缓存分类列表
    private void setCache(EmBNewsType emBNewsType) {
        String cacheKey = "key_";
        if (null != getTypeId()) {
            cacheKey += getTypeId();
        }
        if (null != getTypeName()) {
            cacheKey += getTypeName();
        }

        emBNewsType.setSearchFlag("tag");
        emBNewsType.setCacheKey(cacheKey);
    }

    public Long getTypeId() {
        return typeId;
    }

    public void setTypeId(Long typeId) {
        this.typeId = typeId;
    }

    public String getTypeName() {
        return typeName;
    }

    public void setTypeName(String typeName) {
        this.typeName = typeName;
    }

    public String getGetType() {
        return getType;
    }

    public void setGetType(String getType) {
        this.getType = getType;
    }

    public Long getNewsTypeId() {
        return newsTypeId;
    }

    public void setNewsTypeId(Long newsTypeId) {
        this.newsTypeId = newsTypeId;
    }
}
